//
//  DisountViewController.m
//  PTravel
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 李涵. All rights reserved.
//

#import "DiscountViewController.h"
#import "DiscountDetailViewController.h"

@interface DiscountViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (retain, nonatomic)UICollectionView *myCollection;
@property (retain, nonatomic)NSMutableArray *discountArr;
@property (retain, nonatomic)Choice *choiceV;
@property (assign, nonatomic)NSInteger nextPage;
@property (assign, nonatomic)BOOL isUpLoading;
@property (copy, nonatomic)NSString *type;
@property (copy, nonatomic)NSString *continent;
@property (copy, nonatomic)NSString *country;
@property (copy, nonatomic)NSString *depart;
@property (copy, nonatomic)NSString *time;
@property (retain, nonatomic)UIView *bgView;
@property (retain, nonatomic)MBProgressHUD *hub;
@end

@implementation DiscountViewController

- (void)dealloc
{
    [_myCollection release];
    [_discountArr release];
    [_choiceV release];
    [_type release];
    [_continent release];
    [_country release];
    [_depart release];
    [_time release];
    [_bgView release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    //夜间模式判断
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"night" object:nil];
    
    
}

- (void)changeColor:(NSNotificationCenter *)notification
{
    //NSLog(@"%@",[Night cellColor]);

    self.myCollection.backgroundColor = [Night bgColor];
    [self.myCollection reloadData];
    self.view.backgroundColor = CELLCOLOR;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.nextPage = 1;
    self.type = @"";
    self.continent = @"";
    self.country = @"";
    self.depart = @"";
    self.time = @"";
    
    
    
#pragma mark === collection的创建
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.myCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, BOUNDS.size.width, BOUNDS.size.height-104) collectionViewLayout:layout];
    self.myCollection.dataSource = self;
    self.myCollection.delegate = self;
    [self.myCollection registerClass:[DiscountCollectionViewCell class] forCellWithReuseIdentifier:@"DiscountCollectionViewCell"];
    if ([[[ NSUserDefaults standardUserDefaults] objectForKey:@"states"] isEqualToString:@"1"]) {
        self.myCollection.backgroundColor = [UIColor colorWithWhite:0.821 alpha:1.000];
    }else{
        self.myCollection.backgroundColor = CELLCOLOR;
    }
    self.myCollection.contentInset = UIEdgeInsetsMake(9, 9, 5, 9);
    [self.view addSubview:self.myCollection];
    [layout release];
    [_myCollection release];
    
    self.discountArr = [NSMutableArray array];
    //[self getData:self.nextPage];
    
    self.bgView = [[UIView alloc] initWithFrame:BOUNDS];
    self.bgView.backgroundColor = [UIColor colorWithWhite:0.634 alpha:0.5];
    self.bgView.hidden = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.bgView addGestureRecognizer:tap];
    [self.view addSubview:self.bgView];
    [_bgView release];
    
    self.choiceV = [[Choice alloc] initWithFrame:CGRectMake(0, 0, BOUNDS.size.width, 40)];
    self.choiceV.choiceDelegate = self;
    [self.view addSubview:self.choiceV];
    [_choiceV release];
    
    
    [self addHeader];
    [self addFooter];
    
}

#pragma mark ===tap
- (void)tapAction
{
    [UIView animateWithDuration:0.1 animations:^{
        self.choiceV.frame = CGRectMake(0, 0, BOUNDS.size.width, 40);
        self.choiceV.myTableView.frame = CGRectMake(0, 0, BOUNDS.size.width/2, 0);
        self.choiceV.bigView.frame = CGRectMake(0, 40, BOUNDS.size.width/2, 0);
        self.choiceV.mySmallTableView.frame = CGRectMake(BOUNDS.size.width/2, 0, BOUNDS.size.width/2, 0);
        self.bgView.hidden = YES;
    }];
}

#pragma mark --下拉刷新
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    
    __block DiscountViewController *vc = self;//因为block块里用self会报警告,所以需要将self声明成block变量
    
    // 添加下拉刷新头部控件
    [self.myCollection addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        vc.nextPage = 1;//记录页码的
        vc.isUpLoading = NO;//标记为下拉操作
        [vc getData:vc.nextPage];//重新请求数据

    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.myCollection headerBeginRefreshing];
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    
    //    __unsafe_unretained typeof(self) vc = self;
    //__unsafe_unretained ViewController *vc = self;
    __unsafe_unretained DiscountViewController *vc = self;
    // 添加上拉刷新尾部控件
    [self.myCollection addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        vc.nextPage ++;
        vc.isUpLoading = YES;//标记为上拉操作
        [vc getData:self.nextPage];
        
    }];
}

#pragma mark === cell点击方法
- (void)cellAction
{
    self.hub = [[MBProgressHUD alloc] init];
    self.hub.labelText = @"loading...";
    [self.hub show:YES];
    self.type = self.choiceV.id;
    self.depart = self.choiceV.departStr;
    self.continent = self.choiceV.continent_id;
    self.country = self.choiceV.country_id;
    self.time = self.choiceV.timeStr;

    [self.discountArr removeAllObjects];
    [self getData:self.nextPage];
    [UIView animateWithDuration:0.1 animations:^{
        self.choiceV.frame = CGRectMake(0, 0, BOUNDS.size.width, 40);
        self.choiceV.myTableView.frame = CGRectMake(0, 0, BOUNDS.size.width/2, 0);
        self.choiceV.mySmallTableView.frame = CGRectMake(BOUNDS.size.width/2, 0, BOUNDS.size.width/2, 0);
        self.choiceV.bigView.frame = CGRectMake(0, 40, BOUNDS.size.width/2, 0);
    }];
    self.bgView.hidden = YES;
}

#pragma mark === 四个按钮方法实现
//类型button
- (void)type:(UIButton *)btn
{
    if (!btn.selected) {
        [UIView animateWithDuration:0.1 animations:^{
            self.choiceV.frame = CGRectMake(0, 0, BOUNDS.size.width,BOUNDS.size.height/2+40);
            self.choiceV.bigView.frame = CGRectMake(0, 40, BOUNDS.size.width/2, BOUNDS.size.height/2);
            
            self.choiceV.myTableView.frame = CGRectMake(0, 0, BOUNDS.size.width/2, BOUNDS.size.height/2);
        }];
        self.bgView.hidden = NO;
        
    }else{
        [UIView animateWithDuration:0.1 animations:^{
            self.choiceV.frame = CGRectMake(0, 0, BOUNDS.size.width, 40);
            self.choiceV.myTableView.frame = CGRectMake(0, 0, BOUNDS.size.width/2, 0);
            self.choiceV.mySmallTableView.frame = CGRectMake(BOUNDS.size.width/2, 0, BOUNDS.size.width/2, 0);
            self.choiceV.bigView.frame = CGRectMake(0, 40, BOUNDS.size.width/2, 0);
        }];
        self.bgView.hidden = YES;
        
    }
    self.choiceV.isDes = NO;
    btn.selected = !btn.selected;
}
//出发地
- (void)depart:(UIButton *)btn
{

    if (!btn.selected) {
        [UIView animateWithDuration:0.1 animations:^{
            self.choiceV.frame = CGRectMake(0, 0, BOUNDS.size.width,BOUNDS.size.height/2+40);
            self.choiceV.bigView.frame = CGRectMake(0, 40, BOUNDS.size.width/2, BOUNDS.size.height/2);
            self.choiceV.myTableView.frame = CGRectMake(0, 0, BOUNDS.size.width/2, BOUNDS.size.height/2);
            
             self.bgView.hidden = NO;
        }];
        
    }else{
        [UIView animateWithDuration:0.1 animations:^{
            self.choiceV.frame = CGRectMake(0, 0, BOUNDS.size.width, 40);
            self.choiceV.myTableView.frame = CGRectMake(0, 0, BOUNDS.size.width/2, 0);
            self.choiceV.bigView.frame = CGRectMake(0, 40, BOUNDS.size.width/2, 0);
            self.choiceV.mySmallTableView.frame = CGRectMake(BOUNDS.size.width/2, 0, BOUNDS.size.width/2, 0);
             self.bgView.hidden = YES;
        }];
        
    }
    self.choiceV.isDes = NO;
        btn.selected = !btn.selected;
}
//目的地
- (void)destination:(UIButton *)btn
{
    if (!btn.selected) {
        [UIView animateWithDuration:0.1 animations:^{
            self.choiceV.frame = CGRectMake(0, 0, BOUNDS.size.width,BOUNDS.size.height/2+40);
            self.choiceV.bigView.frame = CGRectMake(0, 40, BOUNDS.size.width, BOUNDS.size.height/2);
            self.choiceV.myTableView.frame = CGRectMake(0, 0, BOUNDS.size.width/2, BOUNDS.size.height/2);
            self.choiceV.mySmallTableView.frame = CGRectMake(BOUNDS.size.width/2, 0, BOUNDS.size.width/2, BOUNDS.size.height/2);
             self.bgView.hidden = NO;
        }];
        
    }else{
        [UIView animateWithDuration:0.1 animations:^{
            self.choiceV.frame = CGRectMake(0, 0, BOUNDS.size.width, 40);
            self.choiceV.myTableView.frame = CGRectMake(0, 0, BOUNDS.size.width/2, 0);
            self.choiceV.bigView.frame = CGRectMake(0, 40, BOUNDS.size.width/2, 0);
            self.choiceV.mySmallTableView.frame = CGRectMake(BOUNDS.size.width/2, 0, BOUNDS.size.width/2, 0);
             self.bgView.hidden = YES;
        }];
        
    }
        btn.selected = !btn.selected;
    self.choiceV.isDes = !self.choiceV.isDes;
    self.choiceV.isDes = YES;
}
//旅行时间
- (void)time:(UIButton *)btn
{
    if (!btn.selected) {
        [UIView animateWithDuration:0.1 animations:^{
            self.choiceV.frame = CGRectMake(0, 0, BOUNDS.size.width,BOUNDS.size.height/2+40);
            self.choiceV.bigView.frame = CGRectMake(0, 40, BOUNDS.size.width/2, BOUNDS.size.height/2);
            self.choiceV.myTableView.frame = CGRectMake(0, 0, BOUNDS.size.width/2, BOUNDS.size.height/2);
             self.bgView.hidden = NO;
        }];
        
    }else{
        [UIView animateWithDuration:0.1 animations:^{
            self.choiceV.frame = CGRectMake(0, 0, BOUNDS.size.width, 40);
            self.choiceV.myTableView.frame = CGRectMake(0, 0, BOUNDS.size.width/2, 0);
            self.choiceV.bigView.frame = CGRectMake(0, 40, BOUNDS.size.width/2, 0);
            self.choiceV.mySmallTableView.frame = CGRectMake(BOUNDS.size.width/2, 0, BOUNDS.size.width/2, 0);
             self.bgView.hidden = YES;
        }];
        
    }
    self.choiceV.isDes = NO;
        btn.selected = !btn.selected;
}


#pragma mark === collection数据请求
- (void)getData:(NSInteger)nextPage
{
    
    [AppTools getAFN:[NSString stringWithFormat:@"http://open.qyer.com/lastminute/app_lastminute_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&continent_id=%@&count=20&country_id=%@&departure=%@&is_show_pay=1&is_show_supplier=1&lat=38.88255639279708&lon=121.5396443687879&max_id=0&oauth_token=7a1da33fff703bde846a4d6980671677&page=%ld&page_size=20&product_type=%@&times=%@&track_app_channel=App%%2520Store&track_app_version=6.8&track_device_info=iPhone7%%2C2&track_deviceid=94E2CF74-5FF7-49B9-8D69-620BF45F65A9&track_os=ios%%25209.1&track_user_id=7017902&v=1", self.continent, self.country, self.depart, self.nextPage, self.type, self.time] block:^(id result) {
        if (self.isUpLoading == NO) {
            [self.discountArr removeAllObjects];
        }
        NSMutableDictionary *bigDic = result;
        NSMutableDictionary *smallDic = [bigDic objectForKey:@"data"];
        NSMutableArray *arr = [smallDic objectForKey:@"lastminutes"];
        for (NSMutableDictionary *dic in arr) {
            Discount *discount = [[Discount alloc] init];
            [discount setValuesForKeysWithDictionary:dic];
            [self.discountArr addObject:discount];
        }
        [self.myCollection reloadData];
        [self.myCollection headerEndRefreshing];
        [self.myCollection footerEndRefreshing];
        self.hub.hidden = YES;
    }];
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"discountArr.count++++++++%ld",self.discountArr.count);
    return self.discountArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DiscountCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscountCollectionViewCell" forIndexPath:indexPath];
    cell.discount = [self.discountArr objectAtIndex:indexPath.row];
    cell.backgroundColor = CELLCOLOR;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(BOUNDS.size.width/2-15, BOUNDS.size.height/3);
}

#pragma mark === item 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DiscountDetailViewController *next = [[DiscountDetailViewController alloc] init];
    [self.navigationController pushViewController:next animated:YES];
    next.url = [NSString stringWithFormat:@"%@",[[self.discountArr objectAtIndex:indexPath.row] url]];
    next.titleName = [NSString stringWithFormat:@"%@",[[self.discountArr objectAtIndex:indexPath.row] title]];
    next.photo = [NSString stringWithFormat:@"%@",[[self.discountArr objectAtIndex:indexPath.row] pic]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
